【问题标题】:image size on device is different after uploaing image on server在服务器上上传图像后,设备上的图像大小不同
【发布时间】:2013-01-23 09:32:42
【问题描述】:

在上传图片之前我正在做以下操作。如果我在上传之前和上传之后检查图像的大小,它会加倍(即如果我上传了 2 MB 的图像,我可以在服务器上看到 4 MB 的图像)。

    NSTimeInterval timeInterval = [assetDate timeIntervalSince1970];
    ALAssetRepresentation *rep = [temp defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    StrPath = [StrPath stringByAppendingFormat:@"%d.%@",(int)timeInterval,strImageType];

    UIImage *image =[UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
    NSData *dataObj = nil;
    dataObj = UIImageJPEGRepresentation(image, 1.0);
    NSString* StrFileData = [Base64 encode:dataObj];
    NSString* strFileHash = [dataObj md5Test];

【问题讨论】:

  • 我猜这取决于编码。
  • CGImageRef iref = [rep fullResolutionImage];这条线无论如何都会影响你认为@AKV
  • 我不确定...您可以更改并检查 Base64 等,也可以尝试通过 UIImageJPEGRepresentation(image, 0.5) 压缩图像
  • 不,我不需要压缩图像,我需要上传前后大小相同
  • 取UIImageView和imgeView.contentMode=UIViewContentModeScaleAspectFit;

标签: iphone objective-c xcode uiimage alassetslibrary


【解决方案1】:

对特定的hightwidth使用以下方法和image

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

这个方法返回NewImage,它不会被resize :)

【讨论】:

    【解决方案2】:

    我有一些建议。如果你注意到这些.. 它应该对你有帮助

    1. 可以直接从 ALAsset defaultRepresentation 获取 ALAsset 图片 NSData

      getBytes:fromOffset:length:error: 方法。

    2. 使用 ALAsset 缩略图而不是检索全分辨率图像

      CGImageRef iref = [myasset aspectRatioThumbnail];

    3. ALAsset Libraay 块将在单独的线程中执行。服务器在主线程中上传也是如此

       dispatch_async(dispatch_get_global_queue(0, 0), ^{
      
         // do the server upload
      
        });
      

    【讨论】:

      猜你喜欢
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2015-06-09
      • 1970-01-01
      • 2011-08-23
      相关资源
      最近更新 更多