我正在尝试在照片应用中调整图片大小但尚未成功。我刚接触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];
}
}
};
答案 0 :(得分:1)
<强>更新强>
您似乎忘了将本地变量scaleImage
设置为scaleImage
类的WUTPostViewController
属性
试试这段代码(最后一行应该成功):
- (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;
...
}
原始答案
您的大小调整算法是正确的。我在我的项目中使用相同的方法,它完美无缺
您似乎向scaleImageToSize:
对象发送了nil
消息。这就是为什么此消息的结果为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
变量。
答案 1 :(得分:0)
请检查第一张图片是否为空
CGSize size1=CGSizeMake(300,300);
UIGraphicsBeginImageContext(size1);
[selectgalleryimage drawInRect:CGRectMake(0, 0,size1.width, size1.height)];
UIImage *SELECTED_IMG1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();