在 iOS 上载到服务器之前调整图像大小
Resizing an image before uploading to server on iOS
我正在尝试在照片应用中调整图像大小,但尚未成功。我是 iOS 上调整照片大小的新手,但我发誓我做对了,但我的日志显示:
Error: <WUTModelImageUploadReq>
[photo]: <nil>
[extension]: <nil>
</WUTModelImageUploadReq>
我正在尝试将 'scaleImage' 设置为 AsyncPostFeed.m
中的图像
这是我的代码:
UIImage+Scaling.m
@implementation UIImage (Scaling)
- (UIImage *)scaleImageToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
CGFloat originX = 0.0;
CGFloat originY = 0.0;
CGRect destinationRect =
CGRectMake(originX, originY, newSize.width, newSize.height);
[self drawInRect:destinationRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
WUTPostViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = _imageForPost;
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
[selectedImage scaleImageToSize:scaleSize];
[self.imageViewForPost setImage:scaleImage];
...
}
WUTPostViewController.h:
@interface WUTPostViewController : WUTCommonViewController <UITextViewDelegate,UIAlertViewDelegate>
@property (strong, nonatomic) IBOutlet UILabel *lblUserName;
@property (strong, nonatomic) IBOutlet UIImageView *imageViewForPost;
@property (strong, nonatomic) IBOutlet UITextView *tvDesription;
@property (strong,nonatomic) UIImage *imageForPost;
@property (strong,nonatomic) UIImage *scaleImage;
- (void)wsPostLikeSuccessCallbackForPostId;
- (void)wsPostLikeFailCallbackWithMessage:(NSString *)errorMessage;
@end
AsyncPostFeed.m:
- (void)uploadPhoto {
WUTModelImageUploadReq *imageUploadReq = [[WUTModelImageUploadReq alloc]init];
// I'm trying to set scaleImage here
imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:UIImageJPEGRepresentation(self.viewControllerPost.scaleImage, 0.07f)]];
imageUploadReq.extension = @"jpg";
NSLog(@"Error: %@", imageUploadReq);
void (^wsSuccessHandler)(AFHTTPRequestOperation *operation, NSDictionary* responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"Pull Feed responseObject %@",responseObject);
NSError *error;
WUTModelPostImageResponse *wsResponse = [[WUTModelPostImageResponse alloc]initWithDictionary:(NSDictionary *)responseObject error:&error];
if (error) {
errorMessage = @"Failure to upload image.";
NSLog(@"Error: %@", error);
[self postExecuteFail];
}else{
if (wsResponse.success) {
WUTModelImage *imageTemp = [wsResponse.data firstObject];
[postItem setObject:imageTemp.photo forKey:@"photo"];
[self uploadPostFeed];
}else{
errorMessage = @"Failure to upload image.";
NSLog(@"Error: %@", error);
[self postExecuteFail];
}
}
};
请检查第一张图片是否为空
CGSize size1=CGSizeMake(300,300);
UIGraphicsBeginImageContext(size1);
[selectgalleryimage drawInRect:CGRectMake(0, 0,size1.width, size1.height)];
UIImage *SELECTED_IMG1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
更新
看起来你忘了将局部变量 scaleImage
设置为 scaleImage
属性 of WUTPostViewController
class.
试试这段代码(最后一行应该可以解决问题):
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = _imageForPost;
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
[selectedImage scaleImageToSize:scaleSize];
[self.imageViewForPost setImage:scaleImage];
self.scaleImage = scaleImage;
...
}
原答案
您的调整大小算法是正确的。我在我的项目中使用了它并且它完美地工作。
看起来您向 nil
对象发送了 scaleImageToSize:
消息。这就是为什么这条消息的结果是 nil
.
您可以通过以下方式更新代码来验证此假设:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = _imageForPost;
NSAssert(selectedImage != nil, @"Selected image is nil");
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
[selectedImage scaleImageToSize:scaleSize];
[self.imageViewForPost setImage:scaleImage];
...
}
如您所见,我添加了 NSAssert
调用,因此如果所选图像为零,您的应用程序将崩溃并显示提供的消息。
如果触发了 NSAssert
,那么您需要查找问题的根本原因。基本上你需要验证 _imageForPost
变量在调用 viewDidLoad
方法之前被初始化。
我正在尝试在照片应用中调整图像大小,但尚未成功。我是 iOS 上调整照片大小的新手,但我发誓我做对了,但我的日志显示:
Error: <WUTModelImageUploadReq>
[photo]: <nil>
[extension]: <nil>
</WUTModelImageUploadReq>
我正在尝试将 'scaleImage' 设置为 AsyncPostFeed.m
中的图像这是我的代码:
UIImage+Scaling.m
@implementation UIImage (Scaling)
- (UIImage *)scaleImageToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
CGFloat originX = 0.0;
CGFloat originY = 0.0;
CGRect destinationRect =
CGRectMake(originX, originY, newSize.width, newSize.height);
[self drawInRect:destinationRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
WUTPostViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = _imageForPost;
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
[selectedImage scaleImageToSize:scaleSize];
[self.imageViewForPost setImage:scaleImage];
...
}
WUTPostViewController.h:
@interface WUTPostViewController : WUTCommonViewController <UITextViewDelegate,UIAlertViewDelegate>
@property (strong, nonatomic) IBOutlet UILabel *lblUserName;
@property (strong, nonatomic) IBOutlet UIImageView *imageViewForPost;
@property (strong, nonatomic) IBOutlet UITextView *tvDesription;
@property (strong,nonatomic) UIImage *imageForPost;
@property (strong,nonatomic) UIImage *scaleImage;
- (void)wsPostLikeSuccessCallbackForPostId;
- (void)wsPostLikeFailCallbackWithMessage:(NSString *)errorMessage;
@end
AsyncPostFeed.m:
- (void)uploadPhoto {
WUTModelImageUploadReq *imageUploadReq = [[WUTModelImageUploadReq alloc]init];
// I'm trying to set scaleImage here
imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:UIImageJPEGRepresentation(self.viewControllerPost.scaleImage, 0.07f)]];
imageUploadReq.extension = @"jpg";
NSLog(@"Error: %@", imageUploadReq);
void (^wsSuccessHandler)(AFHTTPRequestOperation *operation, NSDictionary* responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"Pull Feed responseObject %@",responseObject);
NSError *error;
WUTModelPostImageResponse *wsResponse = [[WUTModelPostImageResponse alloc]initWithDictionary:(NSDictionary *)responseObject error:&error];
if (error) {
errorMessage = @"Failure to upload image.";
NSLog(@"Error: %@", error);
[self postExecuteFail];
}else{
if (wsResponse.success) {
WUTModelImage *imageTemp = [wsResponse.data firstObject];
[postItem setObject:imageTemp.photo forKey:@"photo"];
[self uploadPostFeed];
}else{
errorMessage = @"Failure to upload image.";
NSLog(@"Error: %@", error);
[self postExecuteFail];
}
}
};
请检查第一张图片是否为空
CGSize size1=CGSizeMake(300,300);
UIGraphicsBeginImageContext(size1);
[selectgalleryimage drawInRect:CGRectMake(0, 0,size1.width, size1.height)];
UIImage *SELECTED_IMG1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
更新
看起来你忘了将局部变量 scaleImage
设置为 scaleImage
属性 of WUTPostViewController
class.
试试这段代码(最后一行应该可以解决问题):
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = _imageForPost;
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
[selectedImage scaleImageToSize:scaleSize];
[self.imageViewForPost setImage:scaleImage];
self.scaleImage = scaleImage;
...
}
原答案
您的调整大小算法是正确的。我在我的项目中使用了它并且它完美地工作。
看起来您向 nil
对象发送了 scaleImageToSize:
消息。这就是为什么这条消息的结果是 nil
.
您可以通过以下方式更新代码来验证此假设:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = _imageForPost;
NSAssert(selectedImage != nil, @"Selected image is nil");
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
[selectedImage scaleImageToSize:scaleSize];
[self.imageViewForPost setImage:scaleImage];
...
}
如您所见,我添加了 NSAssert
调用,因此如果所选图像为零,您的应用程序将崩溃并显示提供的消息。
如果触发了 NSAssert
,那么您需要查找问题的根本原因。基本上你需要验证 _imageForPost
变量在调用 viewDidLoad
方法之前被初始化。