ios - 在上传到 iOS 上的服务器之前调整图像大小
<p><p>我正在尝试在照片应用中调整图像大小,但尚未成功。我是在 iOS 上调整照片大小的新手,但我发誓我做对了,但我的日志显示:</p>
<pre><code>Error: <WUTModelImageUploadReq>
: <nil>
: <nil>
</WUTModelImageUploadReq>
</code></pre>
<p>我正在尝试将“scaleImage”设置为 AsyncPostFeed.m 中的图像</p>
<p>这是我的代码:</p>
<p>UIImage+Scaling.m</p>
<pre><code>@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);
;
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
</code></pre>
<p>WUTPostViewController.m:</p>
<pre><code>- (void)viewDidLoad {
;
UIImage *selectedImage = _imageForPost;
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
;
;
...
}
</code></pre>
<p>WUTPostViewController.h:</p>
<pre><code>@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
</code></pre>
<p>AsyncPostFeed.m:</p>
<pre><code>- (void)uploadPhoto {
WUTModelImageUploadReq *imageUploadReq = [init];
// I'm trying to set scaleImage here
imageUploadReq.photo = ];
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 = [initWithDictionary:(NSDictionary *)responseObject error:&error];
if (error) {
errorMessage = @"Failure to upload image.";
NSLog(@"Error: %@", error);
;
}else{
if (wsResponse.success) {
WUTModelImage *imageTemp = ;
;
;
}else{
errorMessage = @"Failure to upload image.";
NSLog(@"Error: %@", error);
;
}
}
};
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>更新</strong><br/>
看起来您忘记将局部变量 <code>scaleImage</code> 设置为 <code>WUTPostViewController</code> 类的 <code>scaleImage</code> 属性。<br/>
试试这个代码(最后一行应该是诀窍):</p>
<pre><code>- (void)viewDidLoad {
;
UIImage *selectedImage = _imageForPost;
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
;
;
self.scaleImage = scaleImage;
...
}
</code></pre>
<p><strong>原答案</strong><br/>
您的调整大小算法是正确的。我在我的项目中使用了相同的方法,并且效果很好。<br/>
看起来您将 <code>scaleImageToSize:</code> 消息发送到 <code>nil</code> 对象。这就是为什么这条消息的结果是 <code>nil</code>。<br/>
你可以通过这种方式更新代码来验证这个假设:</p>
<pre><code>- (void)viewDidLoad {
;
UIImage *selectedImage = _imageForPost;
NSAssert(selectedImage != nil, @"Selected image is nil");
CGSize scaleSize = CGSizeMake(200.0f, 200.0f);
UIImage *scaleImage =
;
;
...
}
</code></pre>
<p>如您所见,我添加了 <code>NSAssert</code> 调用,因此如果所选图像为 nil,您的应用程序将因提供的消息而崩溃。<br/>
如果 <code>NSAssert</code> 被触发,那么您需要查找问题的根本原因。基本上,您需要在调用 <code>viewDidLoad</code> 方法之前验证 <code>_imageForPost</code> 变量是否已初始化。</p></p>
<p style="font-size: 20px;">关于ios - 在上传到 iOS 上的服务器之前调整图像大小,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31713311/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31713311/
</a>
</p>
页:
[1]