2013-06-10 34 views
19

hi Tôi muốn gửi một hình ảnh đến một dịch vụ web. Nhưng tôi muốn thay đổi kích thước người dùng đã chọn bất kỳ hình ảnh nào thành 75 x 75 và gửi cho dịch vụ web. Làm thế nào tôi có thể thay đổi kích thước hình ảnh này vào 75 x 75Làm cách nào để thay đổi kích thước hình ảnh trong iOS?

Cảm ơn

+0

http://stackoverflow.com/questions/10491080/uiimage-resizing-not-working-right/10491692 # 10491692 kiểm tra câu trả lời tuyệt vời này bởi Rob – pmk

Trả lời

38

Cố gắng thực hiện các mã dưới đây. Nó có thể hữu ích cho bạn:

CGRect rect = CGRectMake(0,0,75,75); 
    UIGraphicsBeginImageContext(rect.size); 
    [yourCurrentOriginalImage drawInRect:rect]; 
    UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    NSData *imageData = UIImagePNGRepresentation(picture1); 
    UIImage *img=[UIImage imageWithData:imageData]; 
+0

hi thanks for thr rply ,, Nếu tôi gửi ở định dạng JPG, cách thay đổi 'UIImagePNGRepresentation' – iDia

+0

này, bạn có thể sử dụng giao diện người dùng ImageJPEGĐại diện, nó cần chất lượng trong phao. – ilmetu

+0

Tại sao bạn thêm bước bổ sung mà bạn tạo dữ liệu PNG chỉ để tạo lại hình ảnh mới? – rckoenes

9

thử mã này:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
32

Sử dụng phương pháp này để thay đổi kích thước hình ảnh của bạn:

+(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize 
{ 
    CGFloat scale = [[UIScreen mainScreen]scale]; 
    /*You can remove the below comment if you dont want to scale the image in retina device .Dont forget to comment UIGraphicsBeginImageContextWithOptions*/ 
    //UIGraphicsBeginImageContext(newSize); 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, scale); 
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
Các vấn đề liên quan