OStack程序员社区-中国程序员成长平台

标题: ios - 在上传到 iOS 上的服务器之前调整图像大小 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:28
标题: ios - 在上传到 iOS 上的服务器之前调整图像大小

我正在尝试在照片应用中调整图像大小,但尚未成功。我是在 iOS 上调整照片大小的新手,但我发誓我做对了,但我的日志显示:

Error: <WUTModelImageUploadReq> 
   [photo]: <nil>
   [extension]: <nil>
</WUTModelImageUploadReq>

我正在尝试将“scaleImage”设置为 AsyncPostFeed.m 中的图像

这是我的代码:

UIImage+Scaling.m

@implementation UIImage (Scaling)

- (UIImage *)scaleImageToSizeCGSize)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)wsPostLikeFailCallbackWithMessageNSString *)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(@"ull Feed responseObject %@",responseObject);

    NSError *error;
    WUTModelPostImageResponse *wsResponse = [[WUTModelPostImageResponse alloc]initWithDictionaryNSDictionary *)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];
        }
     }
  };



Best Answer-推荐答案


更新
看起来您忘记将局部变量 scaleImage 设置为 WUTPostViewController 类的 scaleImage 属性。
试试这个代码(最后一行应该是诀窍):

- (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 调用,因此如果所选图像为 nil,您的应用程序将因提供的消息而崩溃。
如果 NSAssert 被触发,那么您需要查找问题的根本原因。基本上,您需要在调用 viewDidLoad 方法之前验证 _imageForPost 变量是否已初始化。

关于ios - 在上传到 iOS 上的服务器之前调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31713311/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4