+ Original Filename of picked image is used as attachment name.

This commit is contained in:
Moritz Haarmann 2014-02-17 23:49:49 +01:00
parent da2c8268d9
commit 6b09e75cd6
6 changed files with 47 additions and 45 deletions

View File

@ -50,12 +50,12 @@
@property (nonatomic, weak) BITFeedbackManager *manager; @property (nonatomic, weak) BITFeedbackManager *manager;
@property (nonatomic, strong) UITextView *textView; @property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIView *contentViewContainer; @property (nonatomic, strong) UIView *contentViewContainer;
@property (nonatomic, strong) UIScrollView *photoScrollView; @property (nonatomic, strong) UIScrollView *attachmentScrollView;
@property (nonatomic, strong) NSMutableArray *photoScrollViewImageViews; @property (nonatomic, strong) NSMutableArray *attachmentScrollViewImageViews;
@property (nonatomic, strong) NSString *text; @property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSMutableArray *photos; @property (nonatomic, strong) NSMutableArray *attachments;
@property (nonatomic, strong) UIView *textAccessoryView; @property (nonatomic, strong) UIView *textAccessoryView;
@ -76,8 +76,8 @@
_blockUserDataScreen = NO; _blockUserDataScreen = NO;
_delegate = nil; _delegate = nil;
_manager = [BITHockeyManager sharedHockeyManager].feedbackManager; _manager = [BITHockeyManager sharedHockeyManager].feedbackManager;
_photos = [NSMutableArray new]; _attachments = [NSMutableArray new];
_photoScrollViewImageViews = [NSMutableArray new]; _attachmentScrollViewImageViews = [NSMutableArray new];
_text = nil; _text = nil;
} }
@ -185,12 +185,12 @@
self.textView.inputAccessoryView = self.textAccessoryView; self.textView.inputAccessoryView = self.textAccessoryView;
// This could be a subclass, yet // This could be a subclass, yet
self.photoScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; self.attachmentScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
self.photoScrollView.scrollEnabled = YES; self.attachmentScrollView.scrollEnabled = YES;
self.photoScrollView.bounces = YES; self.attachmentScrollView.bounces = YES;
self.photoScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; self.attachmentScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.contentViewContainer addSubview:self.photoScrollView]; [self.contentViewContainer addSubview:self.attachmentScrollView];
} }
- (void)viewWillAppear:(BOOL)animated { - (void)viewWillAppear:(BOOL)animated {
@ -256,13 +256,13 @@
-(void)refreshPhotoScrollview { -(void)refreshPhotoScrollview {
CGFloat scrollViewWidth = 0; CGFloat scrollViewWidth = 0;
if (self.photos.count){ if (self.attachments.count){
scrollViewWidth = 100; scrollViewWidth = 100;
} }
CGRect textViewFrame = self.textView.frame; CGRect textViewFrame = self.textView.frame;
CGRect scrollViewFrame = self.photoScrollView.frame; CGRect scrollViewFrame = self.attachmentScrollView.frame;
BOOL alreadySetup = CGRectGetWidth(scrollViewFrame) == scrollViewWidth; BOOL alreadySetup = CGRectGetWidth(scrollViewFrame) == scrollViewWidth;
@ -270,19 +270,19 @@
textViewFrame.size.width -= scrollViewWidth; textViewFrame.size.width -= scrollViewWidth;
scrollViewFrame = CGRectMake(CGRectGetMaxX(textViewFrame), self.view.frame.origin.y, scrollViewWidth, CGRectGetHeight(textViewFrame)); scrollViewFrame = CGRectMake(CGRectGetMaxX(textViewFrame), self.view.frame.origin.y, scrollViewWidth, CGRectGetHeight(textViewFrame));
self.textView.frame = textViewFrame; self.textView.frame = textViewFrame;
self.photoScrollView.frame = scrollViewFrame; self.attachmentScrollView.frame = scrollViewFrame;
self.photoScrollView.contentInset = self.textView.contentInset; self.attachmentScrollView.contentInset = self.textView.contentInset;
} }
for (UIView *subview in self.photoScrollView.subviews){ for (UIView *subview in self.attachmentScrollView.subviews){
[subview removeFromSuperview]; [subview removeFromSuperview];
} }
if (self.photos.count > self.photoScrollViewImageViews.count){ if (self.attachments.count > self.attachmentScrollViewImageViews.count){
NSInteger numberOfViewsToCreate = self.photos.count - self.photoScrollViewImageViews.count; NSInteger numberOfViewsToCreate = self.attachments.count - self.attachmentScrollViewImageViews.count;
for (int i = 0;i<numberOfViewsToCreate;i++){ for (int i = 0;i<numberOfViewsToCreate;i++){
UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.photoScrollViewImageViews addObject:newImageView]; [self.attachmentScrollViewImageViews addObject:newImageView];
} }
} }
@ -290,25 +290,27 @@
CGFloat currentYOffset = 0.0f; CGFloat currentYOffset = 0.0f;
for (UIImage* photo in self.photos){ for (BITFeedbackMessageAttachment* attachment in self.attachments){
UIImageView *imageView = self.photoScrollViewImageViews[index]; UIImageView *imageView = self.attachmentScrollViewImageViews[index];
UIImage *image = [attachment thumbnailWithSize:CGSizeMake(100, 100)];
// determine the factor by which we scale.. // determine the factor by which we scale..
CGFloat scaleFactor = CGRectGetWidth(self.photoScrollView.frame) / photo.size.width; CGFloat scaleFactor = CGRectGetWidth(self.attachmentScrollView.frame) / image.size.width;
CGFloat height = photo.size.height * scaleFactor; CGFloat height = image.size.height * scaleFactor;
imageView.frame = CGRectInset(CGRectMake(0, currentYOffset, scaleFactor * photo.size.width, height),10,10); imageView.frame = CGRectInset(CGRectMake(0, currentYOffset, scaleFactor * image.size.width, height),10,10);
currentYOffset += height; currentYOffset += height;
[self.photoScrollView addSubview:imageView]; [self.attachmentScrollView addSubview:imageView];
imageView.image = photo; imageView.image = image;
index++; index++;
} }
[self.photoScrollView setContentSize:CGSizeMake(CGRectGetWidth(self.photoScrollView.frame), currentYOffset)]; [self.attachmentScrollView setContentSize:CGSizeMake(CGRectGetWidth(self.attachmentScrollView.frame), currentYOffset)];
} }
@ -343,16 +345,7 @@
NSString *text = self.textView.text; NSString *text = self.textView.text;
// Create attachments from the photos. [self.manager submitMessageWithText:text andAttachments:self.attachments];
NSMutableArray *attachments = [NSMutableArray new];
for (UIImage *photo in self.photos){
BITFeedbackMessageAttachment *attachment = [BITFeedbackMessageAttachment attachmentWithData:UIImageJPEGRepresentation(photo, 0.7f) contentType:@"image/jpeg"];
[attachments addObject:attachment];
}
[self.manager submitMessageWithText:text andAttachments:attachments];
[self dismissWithResult:BITFeedbackComposeResultSubmitted]; [self dismissWithResult:BITFeedbackComposeResultSubmitted];
} }
@ -385,7 +378,12 @@
UIImage *pickedImage = info[UIImagePickerControllerOriginalImage]; UIImage *pickedImage = info[UIImagePickerControllerOriginalImage];
if (pickedImage){ if (pickedImage){
[self.photos addObject:pickedImage]; NSData *imageData = UIImageJPEGRepresentation(pickedImage, 0.7f);
BITFeedbackMessageAttachment *newAttachment = [BITFeedbackMessageAttachment attachmentWithData:imageData contentType:@"image/jpeg"];
NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
newAttachment.originalFilename = imageName;
[self.attachments addObject:newAttachment];
} }
[picker dismissModalViewControllerAnimated:YES]; [picker dismissModalViewControllerAnimated:YES];

View File

@ -849,7 +849,7 @@
for (BITFeedbackMessageAttachment *attachment in message.attachments){ for (BITFeedbackMessageAttachment *attachment in message.attachments){
NSString *key = [NSString stringWithFormat:@"attachment%ld", (long)photoIndex]; NSString *key = [NSString stringWithFormat:@"attachment%ld", (long)photoIndex];
[postBody appendData:[BITHockeyAppClient dataWithPostValue:attachment.data forKey:key contentType:attachment.contentType boundary:boundary]]; [postBody appendData:[BITHockeyAppClient dataWithPostValue:attachment.data forKey:key contentType:attachment.contentType boundary:boundary filename:attachment.originalFilename]];
photoIndex++; photoIndex++;
} }

View File

@ -32,8 +32,8 @@
@interface BITFeedbackMessageAttachment : NSObject<NSCoding> @interface BITFeedbackMessageAttachment : NSObject<NSCoding>
@property (nonatomic, copy) NSString *filename;
@property (nonatomic, copy) NSNumber *id; @property (nonatomic, copy) NSNumber *id;
@property (nonatomic, copy) NSString *originalFilename;
@property (nonatomic, copy) NSString *contentType; @property (nonatomic, copy) NSString *contentType;
@property (nonatomic, readonly) NSData *data; @property (nonatomic, readonly) NSData *data;

View File

@ -37,6 +37,8 @@
@property (nonatomic, strong) NSMutableDictionary *thumbnailRepresentations; @property (nonatomic, strong) NSMutableDictionary *thumbnailRepresentations;
@property (nonatomic, strong) NSData *internalData; @property (nonatomic, strong) NSData *internalData;
@property (nonatomic, copy) NSString *filename;
@end @end
@implementation BITFeedbackMessageAttachment @implementation BITFeedbackMessageAttachment
@ -79,6 +81,7 @@
- (void)encodeWithCoder:(NSCoder *)aCoder { - (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.contentType forKey:@"contentType"]; [aCoder encodeObject:self.contentType forKey:@"contentType"];
[aCoder encodeObject:self.filename forKey:@"filename"]; [aCoder encodeObject:self.filename forKey:@"filename"];
[aCoder encodeObject:self.originalFilename forKey:@"originalFilename"];
} }
@ -89,6 +92,7 @@
self.contentType = [aDecoder decodeObjectForKey:@"contentType"]; self.contentType = [aDecoder decodeObjectForKey:@"contentType"];
self.filename = [aDecoder decodeObjectForKey:@"filename"]; self.filename = [aDecoder decodeObjectForKey:@"filename"];
self.thumbnailRepresentations = [NSMutableDictionary new]; self.thumbnailRepresentations = [NSMutableDictionary new];
self.originalFilename = [aDecoder decodeObjectForKey:@"originalFilename"];
} }
return self; return self;

View File

@ -138,6 +138,5 @@
* *
* @return NSData instance configured to be attached on a (post) URLRequest * @return NSData instance configured to be attached on a (post) URLRequest
*/ */
+ (NSData *)dataWithPostValue:(NSData *)value forKey:(NSString *)key contentType:(NSString *)contentType boundary:(NSString *) boundary; + (NSData *)dataWithPostValue:(NSData *)value forKey:(NSString *)key contentType:(NSString *)contentType boundary:(NSString *) boundary filename:(NSString *)filename;
@end @end

View File

@ -85,23 +85,24 @@
} }
+ (NSData *)dataWithPostValue:(NSString *)value forKey:(NSString *)key boundary:(NSString *) boundary { + (NSData *)dataWithPostValue:(NSString *)value forKey:(NSString *)key boundary:(NSString *) boundary {
return [self dataWithPostValue:[value dataUsingEncoding:NSUTF8StringEncoding] forKey:key contentType:@"text" boundary:boundary]; return [self dataWithPostValue:[value dataUsingEncoding:NSUTF8StringEncoding] forKey:key contentType:@"text" boundary:boundary filename:nil];
} }
+ (NSData *)dataWithPostValue:(NSData *)value forKey:(NSString *)key contentType:(NSString *)contentType boundary:(NSString *) boundary { + (NSData *)dataWithPostValue:(NSData *)value forKey:(NSString *)key contentType:(NSString *)contentType boundary:(NSString *) boundary filename:(NSString *)filename {
NSMutableData *postBody = [NSMutableData data]; NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// There's certainly a better way to check if we are supposed to send binary data here. // There's certainly a better way to check if we are supposed to send binary data here.
if ([contentType rangeOfString:@"text"].location == NSNotFound){ if (filename){
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
} else { } else {
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
} }
[postBody appendData:value]; [postBody appendData:value];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];